<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<%--应用系统首页 --%>
		<title>${title}</title>
		<link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/common.css'/>"/>
		
		<link rel="stylesheet"  href="<c:url value='/resource/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
		<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/resource/js/jquery.form.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.core-3.2.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.excheck-3.2.min.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.exedit-3.2.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
		<script type="text/javascript">
			var win;
			$(function() {
				//初始化布局
				$.fn.zTree.init($("#treeDemo"), setting);
				initView();
				$("#ifrm_deptlist").attr("src","<c:url value='/user/list/${_SESSION_LOGIN_USER.deptId}/1.html'/>");
			});
			//界面初始化
			function initView(){
				$("#right,#tree_wrap_div").css({"height":$(window).height()});
				$("#ifrm_deptlist").css({"height":$(window).height()-4});
			}
			var setting = {
				async : {
					enable : true,
					url : "<c:url value='/dept/dragtree.json'/>",
					dataType : "json",
					type : 'get',
					autoParam : [ "deptId" ],
					dataFilter : function(treeId, parentNode, childNodes) {
						$.each(childNodes,function(i,node){
							node.drag = true;
						});
						return childNodes.data
					}
				},
				callback : {
					onClick : zTreeOnClick,
					beforeExpand: beforeExpand,
					onExpand: onExpand,
					beforeDrag: beforeDrag,
					beforeDrop: beforeDrop
				},
				data : {
					key:{
						name:"alias"
					},
					simpleData : {
						enable : true,
						idKey : "deptId",
						pIdKey : "parentId",
						rootPId : 0
					}
				},
				edit:{
					enable:true,
					showRemoveBtn:false,
					showRenameBtn:false
				}
			};
		
			//拖拽事件
			function beforeDrag(treeId, treeNodes) {
				for (var i=0,l=treeNodes.length; i<l; i++) {
					if (treeNodes[i].drag === false) {
						return false;
					}
				}
				return true;
			}
			function beforeDrop(treeId, treeNodes, targetNode, moveType) {
				//判断节点是否可以拖拽
				var drogAble = targetNode ? targetNode.drop !== false : true;
				if(drogAble){
					if(treeNodes[0].parentId!=targetNode.parentId){
						art.dialog.tips("必须上级部门一致才能拖动!",1.5);
						return false;
					}
					if(moveType=="inner"){
						art.dialog.tips("不可拖动到非同级部门!",1.5);
						return false;
					}
					var flag = false;
					//后台修改
						$.ajax({
							type:"POST",
							url:"<%=request.getContextPath()%>/dept/updateorder.json",
							async:false,
							data:{
								'deptId':treeNodes[0].deptId,
								'targetDeptId':targetNode.deptId
							},
							success:function(data){
								flag = true;
							},
							error:function(){
								art.dialog.tips("操作失败！",1.5);
							}
						});
					return flag;
				}else{
					return false;
				}
			}
			/**同时只能展开一个节点，减少树的长度 start*/
			var curExpandNode = null;
			function beforeExpand(treeId, treeNode) {
				var pNode = curExpandNode ? curExpandNode.getParentNode():null;
				var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
					if (treeNode !== treeNodeP.children[i]) {
						zTree.expandNode(treeNodeP.children[i], false);
					}
				}
				while (pNode) {
					if (pNode === treeNode) {
						break;
					}
					pNode = pNode.getParentNode();
				}
				if (!pNode) {
					singlePath(treeNode);
				}
			}
		
			function singlePath(newNode) {
				if (newNode === curExpandNode) return;
				if (curExpandNode && curExpandNode.open==true) {
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					if (newNode.parentTId === curExpandNode.parentTId) {
						zTree.expandNode(curExpandNode, false);
					} else {
						var newParents = [];
						while (newNode) {
							newNode = newNode.getParentNode();
							if (newNode === curExpandNode) {
								newParents = null;
								break;
							} else if (newNode) {
								newParents.push(newNode);
							}
						}
						if (newParents!=null) {
							var oldNode = curExpandNode;
							var oldParents = [];
							while (oldNode) {
								oldNode = oldNode.getParentNode();
								if (oldNode) {
									oldParents.push(oldNode);
								}
							}
							if (newParents.length>0) {
								for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
									if (newParents[i] !== oldParents[i]) {
										zTree.expandNode(oldParents[i], false);
										break;
									}
								}
							} else {
								zTree.expandNode(oldParents[oldParents.length-1], false);
							}
						}
					}
				}
				curExpandNode = newNode;
			}

			function onExpand(event, treeId, treeNode) {
				curExpandNode = treeNode;
			}
			
			/**Tree单击事件*/
			function zTreeOnClick(event, treeId, treeNode) {
				$("#ifrm_deptlist").attr("src","<c:url value='/user/list/"+treeNode.deptId+"/1.html'/>");
			}
			
			//新增,编辑 ，删除之后刷新树节点
			function refreshTree(parentId,oper,alias,deptId,name){
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				var node = zTree.getNodeByParam("deptId",parentId,null);
				if(node){
					if('add'==oper){
						var parent = true;
						if(node.isParent===false){
							node.isParent=true;
							parent = false;
						}
							if("3205"==parentId){
								$.fn.zTree.init($("#treeDemo"), setting);
							}else{
								if(node.open||!parent){
									zTree.reAsyncChildNodes(node,"refresh");
								}
							}			
					}
					if('edit'==oper){
						node.alias = alias;
						node.deptId = deptId;
						node.name = name;
						zTree.updateNode(node);
					}
					if("delete"==oper){
						if(parentId=="3205"||"320500"==parentId){
							$.fn.zTree.init($("#treeDemo"), setting);
						}else{
							if(node.open){
								zTree.reAsyncChildNodes(node,"refresh");
							}	
						}
					}
				}else{
					if("320500"==parentId){
							$.fn.zTree.init($("#treeDemo"), setting);
					}
				}
			}
		
			//打开弹出页面窗口
			function openWin(url,oper){
				art.dialog.open(url,{
					title:'组织机构-'+("1"== oper ? "新增":"编辑"),
					lock : true,
					width : 360,
					height : 170,
					fixed : true,
					ok: function () {
						var iframe = this.iframe.contentWindow;
						if(!iframe.document.body){
							art.dialog.tips("正在加载，请稍候...",1.5);
							return false;
						}
						iframe.submitForm();
						return false;
					},
					cancel: true
				});
			}

			function editDept(deptId,pageIndex){
                        if("320500"==deptId){
					art.dialog.tips("请选择部门");
					return;
				}
				var url = "<c:url value='/dept/edit/"+deptId+".html'/>";
					art.dialog.open(url,{
						title:"组织机构编辑",
						lock : true,
						width : 360,
						height : 380,
						fixed : true,
						ok: function () {
							var iframe = this.iframe.contentWindow;
							if(!iframe.document.body){
								art.dialog.tips("正在加载，请稍候...",1.5);
								return false;
							}
							iframe.submitForm(pageIndex);
							return false;
						},
						cancel: true
					});
			}
			//移除部门
			function deleteDept(deptId,pageIndex){
				if("320500"==deptId){
					art.dialog.tips("请选择部门");
					return;
				}
				art.dialog.confirm("是否确认删除当前部门?",function(){
					$.ajax({
						url:"<c:url value='/dept/deletedept/"+deptId+".json'/>",
						success:function(result){
							switch(result.msg){
							case 0:
								art.dialog.tips("删除成功!");
								window.setTimeout(function(){
									window.parent.frames["ifrm_data"].frames["ifrm_deptlist"]._goto(pageIndex);
									//刷新树节点
									window.parent.frames["ifrm_data"].refreshTree('320500','delete');
									art.dialog.close();
								},1500);
								break;
							case 1:
								art.dialog.tips("含有下级部门，删除失败!");
								break;
							case 2:
								art.dialog.tips("部门人员不为空，删除失败!");
								break;
							default :art.dialog.tips("错误");break;
							}
						},
						error:function(){
							art.dialog.tips("删除失败!");
						}
				});
				});
			}
			
			function addDept(deptId,pageIndex){
				if(deptId==""){
					art.dialog.tips("请先在左侧选择上级部门!",1.5);
					return false;
				}
				deptId="320500";
				var url = "<c:url value='/dept/adddept/"+deptId+".html'/>";
				if("320500"==deptId){
					art.dialog.open(url,{
						title:"组织机构新增",
						lock : true,
						width : 360,
						height : 380,
						fixed : true,
						ok: function () {
							var iframe = this.iframe.contentWindow;
							if(!iframe.document.body){
								art.dialog.tips("正在加载，请稍候...",1.5);
								return false;
							}
							iframe.submitForm(pageIndex);
							return false;
						},
						cancel: true
					});
				}else{
					openWin(url,1);
				}
			}
		
			//显示部门用户信息
			function showUser(deptId){
				art.dialog.open(
					"<c:url value='/user/view/condition/@/1.html'/>".replace("@",deptId),{
					title:'显示部门人员',
					lock : true,
					width : 680,
					height : 460,
					fixed : true
				});
			}
			 /**
			   *跳转到新增人员界面
			   */
			 function addUser(deptId,pageIndex){
				 if("320500"==deptId){
					 art.dialog.tips("请先选择部门");
					 return;
				 }
				var url = "<c:url value='/user/gotoadd/"+deptId+".html'/>"
				win = art.dialog.open(url,{
					title:"人员新增",
					lock : true,
					width:360,
					height:250,
					resize:false,
					fixed : true,
					ok: function () {
						var iframe = this.iframe.contentWindow;
						if(!iframe.document.body){
							art.dialog.tips("正在加载，请稍候...",1500);
							return false;
						}
						iframe.submitForm(pageIndex);
						return false;
					},
					cancel: true
				});
			 }
			//用户编辑
			function edit_user(policeNum,pageIndex){
				art.dialog.open("<c:url value='/user/gotoedit/"+policeNum+".html'/>",{
					width:360,
					height:250,
					title:"人员信息编辑",
					lock:true,
					fix:true,
					ok:function(){
						var iframe = this.iframe.contentWindow;
						iframe.submitForm(pageIndex);
						return false;
					},
					cancel:true
				},false);
			}
	</script>
</head>
<body >
	<div>
		<div id="tree_wrap_div" class="zTreeDemoBackground left" style="width:220px;margin:0px;padding:0px;overflow-x:hidden;float:left;overflow-y:auto;background-color:#f2f2f2">
			<ul id="treeDemo" class="ztree" style="width:220px;overflow-x:hidden;"></ul>
		</div>
    	<div id="right" style="margin-left:221px !important;">
			<iframe id="ifrm_deptlist" name="ifrm_deptlist" style="width:100%;height:100%;border:none;" scrolling="no" frameborder="0"></iframe>
    	</div>
   	</div>
</body>
</html>
